﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace Task3
{
    class DBHelper
    {
        public static string companyName ="STTF科技公司"; 


        public static SqlConnection conn = null;
        public static string connStr = "server=.;database=Game;Integrated Security=true";
        //查询，获取DataReader
        public static SqlDataReader Select(string sql)
        {
            //1.创建连接对象
            conn = new SqlConnection(connStr);
            //2.打开
            conn.Open();
            //3.创建命令对象
            SqlCommand cmd = new SqlCommand(sql, conn);
            //4.执行并返回结果
            return cmd.ExecuteReader
           (System.Data.CommandBehavior.CloseConnection);

        }
        //增、删、改操作
        public static bool ExecuteNonQuery(string sql)
        {
            #region
            int result = 0;
            try
            {
                //创建连接对象
                conn = new SqlConnection(connStr);
                //打开
                conn.Open();
                //创建命令对象
                SqlCommand cmd = new SqlCommand(sql, conn);
                //执行并返回结果
                result = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine("捕获的异常信息：" + ex.Message);
            }
            finally
            {
                conn.Close();
            }
            return result > 0;
            #endregion

            
            

        }
        //执行统计函数
        public static object ExecuteScalar(string sql)
        {
            conn = new SqlConnection(connStr);
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            object result = cmd.ExecuteScalar();
            conn.Close();
            return result;

        }
        //静态方法
        public static void ShowCompany()
        {
            Console.WriteLine("{0}欢迎您，我们的企业文化是：",DBHelper.companyName);
            Console.WriteLine("愿景——最受尊敬的IT企业");
            Console.WriteLine("使命——通过互联网服务和技术提升人类生活品质");
            Console.WriteLine("价值观——正直、进取、合作、创新");
        }
    }
}
